package cn.zhoutao.org.service.impl;

import cn.zhoutao.basic.exception.BusinessException;
import cn.zhoutao.basic.util.BaiduAuditUtils;
import cn.zhoutao.org.domain.Employee;
import cn.zhoutao.org.domain.Shop;
import cn.zhoutao.org.domain.ShopAuditLog;
import cn.zhoutao.org.mapper.EmployeeMapper;
import cn.zhoutao.org.mapper.ShopAuditLogMapper;
import cn.zhoutao.org.mapper.ShopMapper;
import cn.zhoutao.org.service.IShopService;
import cn.zhoutao.basic.service.impl.BaseServiceImpl;
import cn.zhoutao.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MrZhou
 * @since 2022-06-12
 *
 *      * 店铺入驻逻辑
 *      *  一：参数空校验
 *      *    。。不能为空
 *      *  二：合法校验   @TODO 百度AI智能审核接口做
 *      *
 *      *  三：判断是否曾经已经注册过
 *      *    1. 店铺不能被注册
 *      *    2.人是否被注册
 *      *      2.1 注册了但是没有绑定店铺
 *      *      2.2 注册了并且已经绑定了店铺
 *      *
 *      *  四：保存数据
 *      *    shop
 *      *    employee  shopId
 *      *
 *      *    反过来修改shop中的admin_id
 *      * @param shop
 *
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        //一：参数空校验 不能为空
        if (StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())){
            throw new BusinessException("内容不能为空！！");

        }

        //店铺名合法校验   百度AI智能审核接口做
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("金桔子！！");
        }
        //1. 店铺不能被注册,判断是否被注册过
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if(shopTmp != null){
            throw new BusinessException("该账户已经注册过，可以直接登陆！");

        }
        //人是否被注册
        Employee empTmp = employeeMapper.loadByUserName(admin.getUsername());
        if(empTmp != null && empTmp.getShop_id()!=null){//说明该注册人已经注册并且已经绑定了店铺
            throw new BusinessException("店铺已经存在，请换人注册！");
        }
        //注册了但是没有绑定店铺
        if(empTmp==null){
            employeeMapper.save(admin);//返回自增id
            empTmp=admin;
        }
        //保存数据
        shop.setAdminId(empTmp.getId());
        shopMapper.save(shop);//返回自增id
        //反过来通过shop_id更改shop中的admin_id
        empTmp.setShop_id(shop.getAdminId());
        employeeMapper.update(empTmp);
    }

    /**
     * 审核通过
     * @param log
     */
    @Override
    public void pass(ShopAuditLog log) {
        //1.修改店铺状态--审核通过
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        //2.记录审核通过日志
        log.setAuditId(log.getShopId());
        shopAuditLogMapper.save(log);
        //发送激活邮件
        Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2294995822@qq.com");
        message.setTo("2294995822@qq.com");
        message.setSubject("宠物乐园店铺入驻激活链接");
        message.setText("亲，你的店铺入驻信息审核通过，请尽快激活使用！："+
                "激活链接：http://localhost:8080/shop/activation/"+log.getShopId());
        javaMailSender.send(message);
    }



    /**
     * 审核驳回
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) {
        //1.修改店铺状态--审核通过
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        //2.记录审核通过日志
        log.setAuditId(334L);
        shopAuditLogMapper.save(log);
        //发送激活邮件
        Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2294995822@qq.com");
        message.setTo(employee.getEmail());
        message.setSubject("宠物乐园店铺入驻修改链接");
        message.setText("亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接："+
                "http://localhost:8081/#/register2?shopId="+log.getShopId());
        javaMailSender.send(message);
    }

    /**
     * 查询不同状态的店铺数量
     * @return
     */
    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }

    /**
     * 导入的批量新增
     * @param shops
     */
    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }

    /**
     * 激活店铺
     * @param shopId
     */
    @Override
    public void activation(Long shopId) {
        //根据店铺id查询出店铺
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(2);
        //修改店铺信息
        shopMapper.update(shop);
    }
}
